<template>
  <PageWrapper title="Tree函数操作示例" contentBackground contentClass="p-4">
    <div class="mb-4">
      <a-button @click="expandAll(true)" class="mr-2"> 展开全部 </a-button>
      <a-button @click="expandAll(false)" class="mr-2"> 折叠全部 </a-button>
      <a-button @click="checkAll(true)" class="mr-2"> 全选 </a-button>
      <a-button @click="checkAll(false)" class="mr-2"> 全不选 </a-button>
      <a-button @click="handleLevel(2)" class="mr-2"> 显示到第2级 </a-button>
      <a-button @click="handleLevel(1)" class="mr-2"> 显示到第1级 </a-button>
    </div>
    <div class="mb-4">
      <a-button @click="handleSetCheckData" class="mr-2"> 设置勾选数据 </a-button>
      <a-button @click="handleGetCheckData" class="mr-2"> 获取勾选数据 </a-button>
      <a-button @click="handleSetSelectData" class="mr-2"> 设置选中数据 </a-button>
      <a-button @click="handleGetSelectData" class="mr-2"> 获取选中数据 </a-button>
      <a-button @click="handleGetSelectNode" class="mr-2"> 获取选中节点 </a-button>

      <a-button @click="handleSetExpandData" class="mr-2"> 设置展开数据 </a-button>
      <a-button @click="handleGetExpandData" class="mr-2"> 获取展开数据 </a-button>
    </div>
    <div class="mb-4">
      <a-button @click="appendNodeByKey(null)" class="mr-2"> 添加根节点 </a-button>
      <a-button @click="appendNodeByKey('2-2')" class="mr-2"> 在 parent3 内添加节点 </a-button>
      <a-button @click="deleteNodeByKey('2-2')" class="mr-2"> 删除 parent3 节点 </a-button>
      <a-button @click="updateNodeByKey('1-1')" class="mr-2"> 更新 parent2 节点 </a-button>
    </div>
    <BasicTree :treeData="treeData" title="函数操作" ref="treeRef" :checkable="true" />
  </PageWrapper>
</template>

<script lang="ts">
import { defineComponent, ref, unref, nextTick } from 'vue';
import { BasicTree, TreeActionType } from '/@/components/Tree/index';
import { treeData } from './data';
import { useMessage } from '/@/hooks/web/useMessage';
import { PageWrapper } from '/@/components/Page';
import { type Nullable } from '@vben/types';

export default defineComponent({
  components: { BasicTree, PageWrapper },
  setup() {
    const treeRef = ref<Nullable<TreeActionType>>(null);
    const { createMessage } = useMessage();

    function getTree() {
      const tree = unref(treeRef);
      if (!tree) {
        throw new Error('tree is null!');
      }
      return tree;
    }

    function handleLevel(level: number) {
      getTree().filterByLevel(level);
    }

    function handleSetCheckData() {
      getTree().setCheckedKeys(['0-0']);
      nextTick(() => {
        console.log('勾选数据已设置，DOM已更新');
      });
    }

    function handleGetCheckData() {
      const keys = getTree().getCheckedKeys();
      createMessage.success(JSON.stringify(keys));
    }

    function handleSetSelectData() {
      getTree().setSelectedKeys(['0-0']);
      nextTick(() => {
        console.log('选中数据已设置，DOM已更新');
      });
    }

    function handleGetSelectData() {
      const keys = getTree().getSelectedKeys();
      createMessage.success(JSON.stringify(keys));
    }

    function handleGetSelectNode() {
      const keys = getTree().getSelectedKeys();
      const node = getTree().getSelectedNode(keys[0]);
      createMessage.success(node !== null ? JSON.stringify(node) : null);
    }

    function handleSetExpandData() {
      getTree().setExpandedKeys(['0-0']);
      nextTick(() => {
        console.log('展开数据已设置，DOM已更新');
      });
    }

    function handleGetExpandData() {
      const keys = getTree().getExpandedKeys();
      createMessage.success(JSON.stringify(keys));
    }

    function checkAll(checkAll: boolean) {
      getTree().checkAll(checkAll);
      nextTick(() => {
        console.log(checkAll ? '全部勾选' : '全部取消勾选', 'DOM已更新');
      });
    }

    function expandAll(expandAll: boolean) {
      getTree().expandAll(expandAll);
      nextTick(() => {
        console.log(expandAll ? '全部展开' : '全部折叠', 'DOM已更新');
      });
    }

    function appendNodeByKey(parentKey: string | null = null) {
      getTree().insertNodeByKey({
        parentKey: parentKey,
        node: {
          title: '新增节点',
          key: '2-2-2',
        },
        // 往后插入
        push: 'push',
      });
      nextTick(() => {
        console.log('节点已新增，DOM已更新');
      });
    }

    function deleteNodeByKey(key: string) {
      getTree().deleteNodeByKey(key);
      nextTick(() => {
        console.log('节点已删除，DOM已更新');
      });
    }

    function updateNodeByKey(key: string) {
      getTree().updateNodeByKey(key, {
        title: 'parent2-new',
      });
      nextTick(() => {
        console.log('节点已更新，DOM已更新');
      });
    }

    return {
      treeData,
      treeRef,
      handleLevel,
      handleSetCheckData,
      handleGetCheckData,
      handleSetSelectData,
      handleGetSelectData,
      handleSetExpandData,
      handleGetExpandData,
      handleGetSelectNode,
      appendNodeByKey,
      deleteNodeByKey,
      updateNodeByKey,
      checkAll,
      expandAll,
    };
  },
});
</script>
